import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { MessageService } from 'src/app/service/message.service';

@Component({
  selector: 'app-e-contract-list',
  templateUrl: './e-contract-list.component.html',
  styleUrls: ['./e-contract-list.component.scss']
})
export class EContractListComponent implements OnInit {

  constructor(
    private http: HttpClient,
    private message: MessageService
  ) { }

  navParams: any = [
    { title: '主页', router: 'home' },
    { title: '业务办理', router: 'business-transaction/nav' },
    { title: '电子合同', router: 'business-transaction/electronic' }
  ];
  last = [
    { title: '电子合同' },
  ]

  colNames = ['业务类型', '办理人', '受理时间', '当前状态'];
  dataSet: any = [];

  signState = ['初始化', '待签署', '个人部分签名','签名完成', '签名失败', '订单过期','订单取消', '订单放弃签署'];

  ngOnInit() {
    this._getContractList();
  }

  _getContractList(pageNumber = 1, pageSize = 5){
    this.nzLoading = true;
    this.getContractList(pageNumber, pageSize).subscribe(res => {
      if(res){
        this.resultList = JSON.parse(JSON.stringify(res.resultList));
        this.nzTotal = res.totalNumber;
        let dataSet: any = [];
        this.resultList.forEach((item: any) => {
          let data = {} as any;
          data.taskName = item.signTaskNew.taskName;
          data.name = item.name;
          data.taskTime = item.signTaskNew.taskTime;
          data.state = this.signState[item.signTaskNew.state];

          data.id = item.id;
          data.sourceFile = item.signTaskNew.sourceFile;
          data.nativeSignFile = item.signTaskNew.nativeSignFile;

          dataSet.push(data);
        })
        this.dataSet = dataSet;
      }

      this.nzLoading = false;
    }, error => {
      console.log(error);
      this.nzLoading = false;
    })
  }
  qrCode = '';
  qrCodeStatus = 0; // 0-空, 1-获取成功, 2-获取失败
  _getSignQrCode(id: string){
    this.qrCode = '';
    this.getSignQrCode(id).subscribe((res: any) => {
      this.qrCodeStatus = 1;
      this.qrCode = res.content;
      this.signSpinning = false;
    }, error => {
      this.qrCodeStatus = 2;
      this.signSpinning = false;
      this.message.createErrorMessage(error);
    })
  }


  

  // 查看合同
  viewContract(data: any){
    this.modalIsVisible = true;
    this.nzSpinning = true;
    let fileId = data.nativeSignFile ? data.nativeSignFile : data.sourceFile;
    let url = `/api/gsxmcp/pb/fm/v1.0/download/${fileId}`;
    this._pdfPreview2(url);

  }
  _pdfPreview2(url: string){
    let f = document.createElement('iframe');
    f.src = 'assets/js/pdfjs/web/viewer.html?file=' + url;
    f.width = '752';
    f.height = '450';

    setTimeout(()=>{
      document.getElementById('pdfpreview')?.appendChild(f);
      
      this.nzSpinning = false;
    }, 0)
  }
  // 合同预览
  modalIsVisible = false;
  nzSpinning = true;
  modalHandleCancel(){
    this.modalIsVisible = false;
  }
  modalHandleOk(){
    this.modalIsVisible = false;
  }

  // 签署
  signContract(data: any){
    if(data.state === '签名完成'){
      this.message.info('已完成签章，不需要再次扫码签署！')
      return
    }
    this.signModalIsVisible = true;
    this.signSpinning = true;
    this._getSignQrCode(data.id);
  }
  // 签署 modal
  signSpinning = true;
  signModalIsVisible = false;
  signModalHandleOk(){
    this.signModalIsVisible = false;
  }
  signModalHandleCancel(){
    this.signModalIsVisible = false;
  }


  // 接口
  getContractList(pageNumber = 1, pageSize = 5): Observable<any>{
    const url = `/api/gsxmcp/ot/si/v1.0/signNew/curUser/page?pageNumber=${pageNumber}&pageSize=${pageSize}`;
    return this.http.get(url);
  }
  getSignQrCode(id: string){
    const url = `/api/gsxmcp/ot/si/v1.0/signNew/QRCode/${id}`;
    return this.http.get(url);
  }



  // 分页
  resultList = []
  nzLoading = true;
  nzTotal = 0;
  pageSizeOptions = [2, 5, 10, 15, 20, 25]; // 页数选择器
  nzPageSize = 5;
  nzPageIndex = 1;
  nzPageSizeChange(){
    this.nzPageIndex = 1;
    this._getContractList(1, this.nzPageSize);
  }
  nzPageIndexChange(){
    this._getContractList(this.nzPageIndex, this.nzPageSize);
  }

}
